<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Prices;
use app\admin\model\Areas;
class Price extends Common{
    public function _initialize()
    {
        parent::_initialize();
        $this->prices = new prices;
    }
    public function index($type=null,$min_mbps=null,$max_mbps=null,$page=null){

    	if($this->request->isPost()){
            $page = (int)$page > 0 ? $page : 1;
            $map=[];
            if(!empty($type)){
                 $map['m.type'] = $type;
            }
            if(!empty($max_mbps)){
            	$map['m.max_mbps'] = $max_mbps;
            }
            if(!empty($min_mbps)){
                $map['m.min_mbps'] = $min_mbps;
            }
            $lists = $this->prices->alias('m')
            	->join('areas a',"a.id=m.city_id","left")
                ->field("m.*,m.type as typelabel,a.name")
                ->order('m.type asc,m.create_time desc')
                ->where($map)
                ->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))
                ->toArray();
            $data = [];
            if($lists['data']){
            	foreach($lists['data'] as $vo){
            		$vo['rmb1'] = "¥ " .intval($vo['rmb1']) ." /".lang('Month') ;
            		$vo['rmb3'] = "¥ " .intval($vo['rmb3']) ." /".lang('Month')  ;
            		$vo['rmb6'] = "¥ " .intval($vo['rmb6']) ." /".lang('Month') ;
            		$vo['rmb12'] = "¥ " .intval($vo['rmb12']) ." /".lang('Month')  ;
            		$vo['usd1'] = "Usd " .intval($vo['usd1']) ." /".lang('Month')  ;
            		$vo['usd3'] = "Usd " .intval($vo['usd3']) ." /".lang('Month')  ;
            		$vo['usd6'] = "Usd " .intval($vo['usd6']) ." /".lang('Month')  ;
            		$vo['usd12'] = "Usd " .intval($vo['usd12']) ." /".lang('Month')  ;
                    $vo['country'] = $vo['country_id'] > 0 ? getCountryOrCityName($vo['country_id']) : '-' ;
                    $vo['mbps'] = $vo['min_mbps']." M ~ ".$vo['max_mbps']." M";
            		$data[] = $vo;
            	}
            }
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$data,'count'=>$lists['total'],'rel'=>1];
        }
        $this->assign('typeList',$this->prices->typeList());
        $this->assign('mbpsList',$this->prices->mbpsList());
        $this->assign('pricemodel',Prices::getPriceModel());
    	return view();
    }
    public function add(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($data['type'] == 3 || $data['type'] == 8){ //國家
                if($this->prices->where(['type'=>$data['type'],'max_mbps'=>$data['max_mbps'],'min_mbps'=>$data['min_mbps'],'country_id'=>$data['country_id'],'city_id'=>$data['city_id']])->count()){
                    return $result = ['code'=>0,'msg'=>lang('This configuration already exists')];
                }
            }else{
                if($this->prices->where(['type'=>$data['type'],'max_mbps'=>$data['max_mbps'],'min_mbps'=>$data['min_mbps']])->count()){
                    return $result = ['code'=>0,'msg'=>lang('This configuration already exists')];
                }
            }
            $result = $this->prices->data($data)->allowField(true)->save();
            if($result){
                return $result = ['code'=>1,'msg'=>lang('The data is saved successfully!')];
            }
            return $result = ['code'=>0,'msg'=>lang('Data save failed!')];
        }
    	$this->assign('typeList',$this->prices->typeList());
    	$this->assign('mbpsList',$this->prices->mbpsList());
    	$this->assign('areas',Areas::getAreas(0));
    	return view('form');
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = $this->request->post();
            $result = $this->prices->data($data)->allowField(true)->isUpdate(true)->save();
            if($result){
                return $result = ['code'=>1,'msg'=>lang('The data is saved successfully!')];
            }
            return $result = ['code'=>0,'msg'=>lang('Data save failed!')];
        }
        $row = db('prices')->where('id',$id)->find();
        $this->assign('row',$row);
        $this->assign('typeList',$this->prices->typeList());
        $this->assign('mbpsList',$this->prices->mbpsList());
        return view();

    }
    public function delete($id=null){
        if($id > 0 && $this->prices->where('id',$id)->delete()){
            return ['code'=>1,'msg'=>lang('Data has been deleted')];
        }
        return ['code'=>0,'msg'=>lang('Delete data failure')];
    }
    /**/
    public function reloadPrice($modeltypes=null,$ids=null){
        
        $midels = Prices::getPriceModel();
        if(!isset($midels[$modeltypes])){
            return ['code'=>0,'msg'=>lang('Interface does not exist')];
        }
        
        $Monthly = new \clt\Monthly($modeltypes);
        switch ($modeltypes) {
            case 'megaport':
                $m['type'] = 9;
                $prices = db('prices')->where($m)->field('id,type,country_id,city_id,max_mbps')->order('type asc,create_time desc')->select();
                if($prices){
                    foreach($prices as $item){
                        $mageport_locationid = Areas::getArealocation('mageport_locationid',['id'=>$item['city_id']]);
                        if(intval($mageport_locationid) > 0){
                            $data = $Monthly->getCloudFastPrice($mageport_locationid,$item['max_mbps']);
                            $result = db('prices')->where(['id'=>$item['id']])->update($data);
                        }
                    }
                }
                break;
            default:
                # code...
                break;
        }
        return ['code'=>1,'msg'=>lang('Configuration fee updated successfully.')]; 
    }
    public function downloadPrice($modeltypes=null){
        $midels = Prices::getPriceModel();
        if(!isset($midels[$modeltypes])){
            return ['code'=>0,'msg'=>lang('Interface does not exist')];
        }
        $Monthly = new \clt\Monthly($modeltypes);
        $file = $Monthly->getAllPrice();
        if(is_file($file)){
            return ['code'=>1,'msg'=>lang('Data has been downloaded.'),'url'=>U("/".$file,[],false)];
        }
        return ['code'=>0,'msg'=>lang('数据下载失败')];
    }
}